package org.example.myleet.rosalind.long1;

import org.example.myleet.rosalind.utils.FastaReader;
import org.example.myleet.rosalind.utils.FastaSequence;

import java.util.List;

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String path = solution.getClass().getResource("").getPath() + "example.txt";
        solution.printAssembly(FastaReader.parseFastaFromFile(path));
    }

    public void printAssembly(List<FastaSequence> sequences) {
        int n = sequences.size();
        //1、先找出一个序列头，这个序列头前半部分不存在于任何一个序列
        StringBuilder res = new StringBuilder();
        String lastSequence = null;
        boolean startFound = false;
        for (int i = 0; i < n; ++i) {
            String sequence = sequences.get(i).getSequence();
            //head是sequence[i]的一半多1bp
            String head = sequence.substring(0, sequence.length() / 2 + 1);
            boolean found = false;
            for (int j = 0; j < n; ++j) {
                if (i != j) {
                    if (sequences.get(j).getSequence().contains(head)) {
                        //如果一个序列包含head，说明sequence[i]的前半部分是与其他序列重叠的，因此不符合一个序列头的特征
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                //找到序列头，加入到结果res中，并从待比对的集合中移除
                lastSequence = sequence;
                res.append(lastSequence);
                startFound = true;
                sequences.remove(i);
                --n;
                break;
            }
        }
        if (!startFound) {
            System.out.println("No head found! Exit...");
            return;
        }
        //2、从序列头的尾部开始，每次找到一个序列是以此为开头的（重叠部分），然后拼接到结果序列上
        while (!sequences.isEmpty()) {
            for (int i = 0; i < n; ++i) {
                String sequence = sequences.get(i).getSequence();
                String head = sequence.substring(0, sequence.length() / 2 + 1);
                //在lastSequence寻找head是否存在
                int idx = lastSequence.indexOf(head);
                if (idx > -1) {
                    //lastSequence中存在head，说明可能找到重叠部分，然后求重叠部分长度，用于截取sequence中的疑似重叠序列
                    int overlapLen = lastSequence.length() - idx;
                    if (lastSequence.endsWith(sequence.substring(0, overlapLen))) {
                        //如果lastSequence是以sequence的疑似重叠部分结尾的，那么我们可以确认就是找到重叠部分了
                        //将非重叠部分追加到结果中，并将sequence[i]移除出待比较集合
                        res.append(sequence.substring(overlapLen));
                        lastSequence = sequence;
                        sequences.remove(i);
                        break;
                    }
                }
            }
            if (sequences.size() == n) {
                System.out.println("All we can find is " + res);
                System.out.println("Cannot find next overlap read! Exit...");
                return;
            }
            --n;
        }
        System.out.println(res);
    }
}
